<template>
  <div class="frame-page">
    <div class="h-panel">
      <div class="flex justify-between p-10px">
        <div class="flex">
          <label for="name" class="mr-10px"></label>
          <Input id="name" v-model="params.name" class="flex-1" placeholder="请输入项目名称"/>
          <Button color="primary" icon="fa fa-search" :loading="loading" @click="doSearch" class="ml-10px">查询</Button>
        </div>
        <div>
          <Button @click="showForm()" color="primary" >添加</Button>
        </div>
      </div>
    </div>
    <vxe-table row-id="id"
               ref="table"
               :data="dataList"
               highlight-hover-row
               show-overflow
               border
               :row-config="{height: 48}"
               :loading="loading">
      <vxe-column type="seq" width="60" title="#"/>
      <vxe-column title="项目编码" field="code" width="100"/>
      <vxe-column title="项目名称" field="name"/>
      <vxe-column title="操作" align="center" width="200">
        <template #default="{row}">
          <div class="flex items-center justify-center">
            <span class=" primary-color text-hover ml-10px" @click="showForm(row)" size="s"><Icon type="h-icon-edit" :size="15" /></span>
            <span class="primary-color ml-10px text-hover" @click="doRemove(row)" size="s"><Icon type="h-icon-trash" :size="15" /></span>
          </div>
        </template>
      </vxe-column>
    </vxe-table>
  </div>
</template>

<script>
import ProjectForm from "./ProjectForm.vue";
import Project from "@js/api/Project";
import {confirm, message} from "heyui";
import {layer} from "@layui/layer-vue";
import {h} from "vue";

/**
 * @功能描述: 项目管理
 * @创建时间: 2023年08月08日
 * @公司官网: www.fenxi365.com
 * @公司信息: 纷析云（杭州）科技有限公司
 * @公司介绍: 专注于财务相关软件开发, 企业会计自动化解决方案
 */
export default {
  name: "ProjectList",
  components: {ProjectForm},
  data() {
    return {
      loading: false,
      params: {
        name: null,
      },
      dataList: [],
    }
  },
  computed: {
    queryParams() {
      return Object.assign(this.params, {})
    }
  },
  methods: {
    showForm(entity) {
      let layerId = layer.open({
        title: "项目信息",
        shadeClose: false,
        closeBtn: false,
        area: ['500px', entity ? '370px' : '430px'],
        content: h(ProjectForm, {
          entity,
          onClose: () => {
            layer.close(layerId);
          },
          onSuccess: () => {
            this.doSearch();
            layer.close(layerId);
          }
        })
      });
    },
    loadList() {
      this.loading = true;
      Project.list(this.queryParams).then(({data}) => {
        this.dataList = data;
      }).finally(() => this.loading = false);
    },
    doSearch() {
      this.loadList();
    },
    doRemove(row) {
      confirm({
        title: "系统提示",
        content: `确认删除用户：${row.name}?`,
        onConfirm: () => {
          Project.remove(row.id).then(() => {
            message("删除成功~");
            this.loadList();
          })
        }
      })
    }
  },
  created() {
    this.loadList();
  }
}
</script>
